package com.example.administrator.zhongyichou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.zhongyichou.net.protocol.MutualMemberProtocol;
import com.example.administrator.zhongyichou.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/12/5.
 * 描述：
 */

public class MutualMembersListAdapter extends ListBaseAdapter {

    private OnChildClickListener onChildClickListener;


    public MutualMembersListAdapter(Context mContext, List list) {
        super(mContext, list);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.mutual_members_list_item1, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MutualMemberProtocol.DataBean bean = (MutualMemberProtocol.DataBean) getData(position);
        holder.txtName.setText(bean.getMember_name());
        holder.txtBalance.setText(bean.getMember_cash() + "元");
        holder.txtOpen.setVisibility(View.VISIBLE);
        switch (bean.getMember_status()) {
            case "0"://未激活
                holder.txtType.setText("未激活");
                holder.txtType.setBackgroundResource(R.drawable.mutual_members_bg2);
                holder.txtBtn1.setText("激活");
                holder.txtBtn2.setVisibility(View.VISIBLE);
                holder.txtOpen.setVisibility(View.GONE);
                break;
            case "1"://已缴费激活-进入考察期
                holder.txtType.setText("观察期");
                holder.txtType.setBackgroundResource(R.drawable.mutual_members_bg3);
                holder.txtBtn1.setText("充值");
                holder.txtBtn2.setVisibility(View.GONE);
                break;
            case "2"://已过考察期-可以发起互助
                holder.txtType.setText("已生效");
                holder.txtType.setBackgroundResource(R.drawable.mutual_members_bg1);
                holder.txtBtn1.setText("充值");
                holder.txtBtn2.setVisibility(View.GONE);
                break;
            case "3"://失效-余额不足，充值后直接进入
                holder.txtType.setText("失效中");
                holder.txtType.setBackgroundResource(R.drawable.mutual_members_bg4);
                holder.txtBtn1.setText("充值");
                holder.txtBtn2.setVisibility(View.GONE);
                break;
        }
        holder.txtBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChildClickListener.onRecharge(position);
            }
        });
        holder.txtBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChildClickListener.onDelete(position);
            }
        });
        holder.txtOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChildClickListener.onOpen(position);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.img_head)
        ImageView imgHead;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.txt_type)
        TextView txtType;
        @BindView(R.id.txt_balance)
        TextView txtBalance;
        @BindView(R.id.txt_btn1)
        TextView txtBtn1;
        @BindView(R.id.txt_btn2)
        TextView txtBtn2;
        @BindView(R.id.txt_open)
        TextView txtOpen;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnChildClickListener {
        void onRecharge(int position);

        void onDelete(int position);

        void onOpen(int position);
    }

    public void setOnChildClickListener(OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }
}
